using static System.Formats.Asn1.AsnWriter;
using System.Media;


namespace 飞机大战
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            //初始化背景
            SingleObject.GetSingle().AddGameObject(new BackGround(0, -850, 5));
            //初始化玩家飞机
            SingleObject.GetSingle().AddGameObject(new PlaneHero(200, 700, 5, 3, Direction.up));
        }

        static Random r = new();

        /// <summary>
        /// 开始时间和结束时间
        /// </summary>
        DateTime dt_start, dt_end;

        /// <summary>
        /// 背景音乐的存储路径
        /// </summary>
        static string music_path = "D:\\CSharp\\飞机大战\\game_music.wav";

        SoundPlayer bgm = new SoundPlayer(music_path);

        /// <summary>
        /// 初始化游戏
        /// </summary>
        public void InitialGame()
        {
            //初始化背景
            SingleObject.GetSingle().AddGameObject(new BackGround(0, -850, 5));
            //初始化玩家飞机
            SingleObject.GetSingle().AddGameObject(new PlaneHero(200, 700, 5, 3, Direction.up));
            //初始化敌人飞机
            InitialPlaneEnemy();
        }

        private void InitialPlaneEnemy()
        {
            for (int i = 0; i < 3; i++)
            {
                SingleObject.GetSingle().AddGameObject(new PlaneEnemy(r.Next(0, this.Width - 380), -250, r.Next(0, 2)));
            }

            //百分之二十的几率出现大飞机
            if (r.Next(0, 100) > 80)
            {
                SingleObject.GetSingle().AddGameObject(new PlaneEnemy(r.Next(0, this.Width - 380), -250, 2));
            }
        }

        /// <summary>
        /// 窗体重绘制时，执行当前事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            SingleObject.GetSingle().Draw(e.Graphics);

            string score = SingleObject.GetSingle().Score.ToString();
            //绘制玩家分数
            e.Graphics.DrawString(score, new Font("宋体", 20, FontStyle.Bold), Brushes.Red, new Point(0, 0));
        }

        private void timerBG_Tick(object sender, EventArgs e)
        {
            //每50毫秒让窗体发生重绘
            this.Invalidate();

            //获取当前敌人飞机的数量
            int count = SingleObject.GetSingle().ListPlaneEnemy.Count;

            //如果当前窗体里敌机数量小于等于1
            if (count <= 1)
            {
                //再次对飞机进行初始化
                InitialPlaneEnemy();
            }

            //不停地碰撞检测
            SingleObject.GetSingle().PZJC();

            //控制游戏时间
            dt_end = DateTime.Now;
            TimeSpan ts = dt_end - dt_start;

            //游戏结束
            if (ts.TotalSeconds >= 60)
            {
                //计时停止
                timerBG.Enabled = false;
                //记录最终分数
                string finalscore = SingleObject.GetSingle().Score.ToString();
                //显示分数及被击中的次数
                lblScore.Text = $"游戏结束！您的分数是{finalscore},被击中{SingleObject.GetSingle().over_count.ToString()}次";
                lblScore.BackColor = Color.DimGray;
                //背景音乐停止播放
                bgm.Stop();
            }
        }

        /// <summary>
        /// 解决闪烁问题
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            //将图像绘制到缓冲区减少闪烁
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer
                | ControlStyles.ResizeRedraw |
                ControlStyles.AllPaintingInWmPaint, true);
        }

        private void FrmMain_MouseMove(object sender, MouseEventArgs e)
        {
            //让飞机跟着鼠标的移动而移动
            SingleObject.GetSingle().PH.mousemove(e);
        }

        private void FrmMain_MouseDown(object sender, MouseEventArgs e)
        {
            //若玩家按下左键，玩家飞机开炮
            SingleObject.GetSingle().PH.Fire();
        }

        /// <summary>
        /// 点击“开始游戏”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStartGame_Click(object sender, EventArgs e)
        {
            InitialGame();
            //计时开始
            timerBG.Enabled = true;
            //记录开始时间
            dt_start = DateTime.Now;
            //背景音乐循环播放
            bgm.PlayLooping();
            //清空lblScore的内容
            lblScore.Text = "";
            lblScore.BackColor = Color.Transparent;
            //分数清零
            SingleObject.GetSingle().Score = 0;
            //被击中次数清零
            SingleObject.GetSingle().over_count = 0;
        }

        /// <summary>
        /// 点击“关于我”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }
    }
}